#include"Queue.h"
void QueueInit(Queue* q)
{
	assert(q);
	q->phead = q->tail = NULL;
	q->size = 0;
}
void QueuePush(Queue* q, QDataType x)
{
	assert(q);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return -1;
	}
	newnode->data = x;
	newnode->next = NULL;
	if (q->tail == NULL)
	{
		q->phead = q->tail = newnode;
	}
	else
	{
		q->tail->next = newnode;
		q->tail = newnode;
	}
	q->size++;
}
void QueuePop(Queue* q)
{
	assert(q);
	assert(q->phead);
	QNode* cur = q->phead;
	q->phead = q->phead->next;
	free(cur);
	cur = NULL;
	if (q->phead == NULL)
		q->tail = NULL;
	q->size--;
}
QDataType QueueFront(Queue* q)
{
	assert(q);
	assert(q->phead);
	return q->phead->data;
}
QDataType QueueBack(Queue* q)
{
	assert(q);
	assert(q->tail);
	return q->tail->data;
}
int QueueSize(Queue* q)
{
	assert(q);
	return q->size;
}
int QueueEmpty(Queue* q)
{
	assert(q);
	return q->phead == NULL;
}
void QueueDestroy(Queue* q)
{
	assert(q);
	QNode* cur = q->phead;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	q->phead = q->tail = NULL;
	q->size = 0;
}